package org.real.guetshop.service.impl;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.constant.MessageConstant;
import org.real.guetshop.context.BaseContext;
import org.real.guetshop.exception.AccountIsExitException;
import org.real.guetshop.exception.AccountNotExitException;
import org.real.guetshop.exception.PasswordErrorException;
import org.real.guetshop.exception.ValifyCodeException;
import org.real.guetshop.mapper.AddressMapper;
import org.real.guetshop.mapper.UserMapper;
import org.real.guetshop.pojo.dto.*;
import org.real.guetshop.pojo.entity.User;
import org.real.guetshop.service.UserService;
import org.real.guetshop.utils.EmailUtil;
import org.real.guetshop.utils.SHAUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    private static final String TX = "https://hgy-project.oss-cn-hangzhou.aliyuncs.com/ce0b9d23-e6f5-405f-8c6d-6f6be2ca04ce.jpg";

    @Resource
    UserMapper userMapper;

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private EmailUtil emailUtil;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 用户登录
     * @param uerLoginDTO
     */
    @SneakyThrows
    @Override
    public User login(UserLoginDTO uerLoginDTO) {
        User user = userMapper.getByUsername(uerLoginDTO.getUsername());

        //处理一些逻辑
        if (user == null) {
            throw new AccountNotExitException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

//        //密码比对
//        if (!user.getPassword().equals(uerLoginDTO.getPassword())) {
//            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
//        }

        //验证密码
        SHAUtils.PasswordHash passwordHash = new SHAUtils.PasswordHash(user.getPassword(), user.getSalt());
        if (!SHAUtils.verifyPassword(uerLoginDTO.getPassword(),passwordHash)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //返回实体对象
        return user;
    }

    /**
     * 用户注册
     * @param userRegisterDTO
     */
    @Override
    public void insert(UserRegisterDTO userRegisterDTO) {

        User isExitUser = userMapper.getByUsername(userRegisterDTO.getUsername());
        if (isExitUser != null && isExitUser.getUsername().equals(userRegisterDTO.getUsername())) {
            throw new AccountIsExitException(MessageConstant.ALREADY_EXISTS);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegisterDTO, user);
        user.setTx(TX);

        //对用户注册的密码进行加密处理
        SHAUtils.PasswordHash passwordHash = SHAUtils.encodePasswordWithSalt(userRegisterDTO.getPassword());
        user.setPassword(passwordHash.getHash());
        user.setSalt(passwordHash.getSalt());

        userMapper.insert(user);
    }

    /**
     * 根据id获取用户信息
     * @param userId
     * @return
     */
    @Override
    public User getUserById(Integer userId) {
        return userMapper.getUserById(userId);
    }

    /**
     * 用户信息更新
     * @param userUpdateDTO
     */
    @Override
    public void update(UserUpdateDTO userUpdateDTO) {
        userMapper.update(userUpdateDTO);
    }

    /**
     * 用户修改密码
     * @return
     */
    @SneakyThrows
    @Override
    public void editPw(EditPwDTO editPwDTO) {

        User user = new User();
        BeanUtils.copyProperties(editPwDTO, user);

        //对用户注册的密码进行加密处理
        SHAUtils.PasswordHash passwordHash = SHAUtils.encodePasswordWithSalt(editPwDTO.getPassword());
        user.setPassword(passwordHash.getHash());
        user.setSalt(passwordHash.getSalt());

        user.setId(BaseContext.getCurrentId());

        userMapper.editPw(user);
    }

}
